package cn.stylefeng.guns.modular.gridsystem.controller;

import cn.stylefeng.guns.core.annotion.BusinessLog;
import cn.stylefeng.guns.core.annotion.DataScope;
import cn.stylefeng.guns.core.context.login.LoginContextHolder;
import cn.stylefeng.guns.core.enums.LogAnnotionOpTypeEnum;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.core.pojo.response.SuccessResponseData;
import cn.stylefeng.guns.modular.gridsystem.entity.BizStation;
import cn.stylefeng.guns.modular.gridsystem.model.param.DirectoryTreeParam;
import cn.stylefeng.guns.modular.gridsystem.model.result.BizStationResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import cn.stylefeng.guns.modular.gridsystem.service.BizStationService;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizStationParam;
import javax.annotation.Resource;
import java.util.*;

/**
 * 站点表 控制器
 *
 * @author: ssy
 * @date: 2021/09/14 11:47
 */
@RestController
public class BizStationController {

    @Resource
    private BizStationService bizStationService;

    /**
     * 分页查询站点表
     *
     * @author ssy
     * @date 2021/09/14 11:47
     */
    @GetMapping("/bizStation/page")
    @DataScope
    @BusinessLog(title = "站点表_分页查询", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData page(BizStationParam bizStationParam) {
        return new SuccessResponseData(bizStationService.page(bizStationParam));
    }

    /**
     * 获取全部站点表
     *
     * @author ssy
     * @date 2021/09/14 11:47
     */
    @GetMapping("/bizStation/list")
    @BusinessLog(title = "站点表_查询所有", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData list(BizStationParam bizStationParam) {
        return new SuccessResponseData(bizStationService.list(bizStationParam));
    }

    /**
     * 查看详情站点表
     *
     * @author ssy
     * @date 2021/09/14 11:47
     */
    @GetMapping("/bizStation/detail")
    @BusinessLog(title = "站点表_查看详情", opType = LogAnnotionOpTypeEnum.DETAIL)
    public ResponseData detail(@Validated(BizStationParam.detail.class) BizStationParam bizStationParam) {
        return new SuccessResponseData(bizStationService.detail(bizStationParam));
    }

    /**
     * 添加站点表
     *
     * @author ssy
     * @date 2021/09/14 11:47
     */
    @PostMapping("/bizStation/add")
    @BusinessLog(title = "站点表_增加", opType = LogAnnotionOpTypeEnum.ADD)
    public ResponseData add(@RequestBody @Validated(BizStationParam.add.class) BizStationParam bizStationParam) {
        boolean superAdmin = LoginContextHolder.me().isSuperAdmin();
        if(superAdmin){
            throw new ServiceException(500,"超级管理员账号禁止添加电站！");
        }
        bizStationService.add(bizStationParam);
        return new SuccessResponseData();
    }

    /**
     * 删除站点表
     *
     * @author ssy
     * @date 2021/09/14 11:47
     */
    @PostMapping("/bizStation/delete")
    @BusinessLog(title = "站点表_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public ResponseData delete(@RequestBody @Validated(BizStationParam.delete.class) BizStationParam bizStationParam) {
        bizStationService.delete(bizStationParam);
        return new SuccessResponseData();
    }

    /**
     * 编辑站点表
     *
     * @author ssy
     * @date 2021/09/14 11:47
     */
    @PostMapping("/bizStation/edit")
    @BusinessLog(title = "站点表_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public ResponseData edit(@RequestBody @Validated(BizStationParam.edit.class) BizStationParam bizStationParam) {
        bizStationService.edit(bizStationParam);
        return new SuccessResponseData();
    }

    /**
     * 获取组织机构树
     *
     * @author xuyuxiang
     * @date 2020/3/26 11:55
     */
    @GetMapping("/bizStation/tree")
    public ResponseData tree(BizStationParam bizStationParam) {
        List<BizStationResult> list = bizStationService.list(bizStationParam);
        DirectoryTreeParam treeParam = new DirectoryTreeParam();
        treeParam.setId("000");
        treeParam.setTitle("全部站点");
        treeParam.setDisabled(true);
        treeParam.setKey("0-0");
        List<DirectoryTreeParam> treeParamList = new ArrayList<>();
        for (BizStationResult bizStationResult : list) {
            DirectoryTreeParam treeParam1 = new DirectoryTreeParam();
            treeParam1.setId(bizStationResult.getId().toString());
            treeParam1.setTitle(bizStationResult.getStationName());
            treeParam1.setDisabled(false);
            treeParam1.setKey(bizStationResult.getId().toString());
            treeParamList.add(treeParam1);
        }
        treeParam.setChildren(treeParamList);
        List<DirectoryTreeParam> treeParamList1 = new ArrayList<>();
        treeParamList1.add(treeParam);
        return new SuccessResponseData(treeParamList1);
    }
}
